package com.zaaach.citypicker.util;


import ohos.aafwk.ability.Ability;
import ohos.agp.utils.Point;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.agp.window.service.Window;
import ohos.app.Context;

import java.util.Optional;

/**
 * @Author: Bro0cL
 * @Date: 2018/12/4 11:35
 * @Discription:
 */
public class ScreenUtil {



    public static int getStatusBarHeight(Context context){
        return 66;
    }

//    public static int getNavigationBarHeight(Context context) {
//        boolean mInPortrait = context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
//        int result = 0;
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
//            if (hasNavBar((Activity) context)) {
//                String key;
//                if (mInPortrait) {
//                    key = "navigation_bar_height";
//                } else {
//                    key = "navigation_bar_height_landscape";
//                }
//                return getInternalDimensionSize(context, key);
//            }
//        }
//        return result;
//    }

    private static boolean hasNavBar(Ability activity) {
        //其他手机根据屏幕真实高度与显示高度是否相同来判断
        DisplayManager instance = DisplayManager.getInstance();
        Optional<Display> defaultDisplay = instance.getDefaultDisplay(activity);
        Point realPoint = new Point();
        defaultDisplay.get().getRealSize(realPoint);


        int realHeight = (int) realPoint.getPointX();
        int realWidth = (int) realPoint.getPointY();

        Point displaylPoint = new Point();
        defaultDisplay.get().getSize(displaylPoint);

        int displayHeight = (int) realPoint.getPointX();
        int displayWidth = (int) realPoint.getPointY();

        return (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
    }
    public static DisplayAttributes getScreenPiex(Context context){
        // 获取屏幕密度
        Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(context);
        DisplayAttributes displayAttributes = display.get().getAttributes();
        //displayAttributes.xDpi;
        //displayAttributes.yDpi
        return  displayAttributes;
    }

    public static int dp2px(Context context, int dp){
        // 获取屏幕密度
        DisplayAttributes displayAttributes = getScreenPiex(context);
        return  (int) (dp * displayAttributes.scalDensity);
    }
    public static int sp2px(Context context, int sp) {
        DisplayAttributes displayAttributes = getScreenPiex(context);
        return  (int)(sp * displayAttributes.scalDensity);
    }
}
